/*
' Copyright (c) 2010  DotNetNuke Corporation
'  All rights reserved.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' 
*/

using System;
using System.Data;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Modules.dnnsimplearticle.Components;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Security;
using Microsoft.ApplicationBlocks.Data;


namespace DotNetNuke.Modules.SimpleArticleImporter
{

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The ViewSimpleArticleImporter class displays the content
    /// </summary>
    /// -----------------------------------------------------------------------------
    public partial class View : SimpleArticleImporterModuleBase, IActionable
    {

        #region Event Handlers

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
        }


        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Page_Load runs when the control is loaded
        /// </summary>
        /// -----------------------------------------------------------------------------
        private void Page_Load(object sender, System.EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    var mc = new ModuleController();
                    ddlModules.DataTextField = "ModuleTitle";
                    ddlModules.DataValueField = "ModuleId";
                    ddlModules.DataSource = mc.GetModulesByDefinition(PortalId, "dnnsimplearticle");
                    ddlModules.DataBind();
                }
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        #endregion

        #region Optional Interfaces

        public ModuleActionCollection ModuleActions
        {
            get
            {
                ModuleActionCollection Actions = new ModuleActionCollection();
                Actions.Add(GetNextActionID(), Localization.GetString("EditModule", this.LocalResourceFile), "", "", "", EditUrl(), false, SecurityAccessLevel.Edit, true, false);
                return Actions;
            }
        }

        #endregion

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                //select TITLE, BODY, CREATEDDATE

                var dr = SqlHelper.ExecuteReader(txtConnectionString.Text.Trim(), CommandType.Text, txtSqlString.Text.Trim());

                while (dr.Read())
                {
                    var a = new Article();
                    a.Title = dr[0].ToString();

                    var description = HtmlUtils.Clean((dr[1].ToString()), false);
                    if (description.Length > 500)
                        a.Description = description.Substring(0, 500);
                    else
                        a.Description = description;

                    a.Body = dr[1].ToString().Trim();
                    a.CreatedByUserId = UserId;
                    a.CreatedOnDate = Convert.ToDateTime(dr[2].ToString());
                    a.LastModifiedByUserId = UserId;
                    a.LastModifiedOnDate = DateTime.Now;
                    a.ModuleId = Convert.ToInt32(ddlModules.SelectedValue);
                    var mc = new ModuleController();
                    var m = mc.GetModule(Convert.ToInt32(ddlModules.SelectedValue));
                    ArticleController.SaveArticle(a, m.TabID);
                }
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }

        }
    }

}
